package com.ruoyi.web.operate.stock.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.web.publish.resume.domain.Resume;
import com.ruoyi.web.publish.resume.service.IResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.operate.stock.mapper.StockMapper;
import com.ruoyi.web.operate.stock.domain.Stock;
import com.ruoyi.web.operate.stock.service.IStockService;

/**
 * 人才股票Service业务层处理
 *
 * @author liuyu
 * @date 2021-08-25
 */
@Service
public class StockServiceImpl implements IStockService {
    @Autowired
    private StockMapper stockMapper;
    @Autowired
    private IResumeService iResumeService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询人才股票
     *
     * @param id 人才股票主键
     * @return 人才股票
     */
    @Override
    public Stock selectStockById(Long id) {
        return stockMapper.selectStockById(id);
    }

    /**
     * 根据购买人ID查询持有人的对应股票
     *
     * @param createById 购买人ID
     * @return 资产集合
     */
    @Override
    public List<Stock> selectHoldStockList() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long createById = loginUser.getUserId();
        return stockMapper.selectHoldStockList(createById);
    }

    /**
     * 根据发行人id查股票信息
     *
     *
     * @return 资产集合
     */
    @Override
    public  List<Stock> selectUpdateStockList() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long owner = loginUser.getUserId();
        return stockMapper.selectUpdateStockList(owner);
    }

    /**
     * 根据拥有人ID查询的人才股票
     *
     * @param owner 所有人ID
     * @return 人才股票
     */
    @Override
    public Stock selectStockByOwner(Long owner) {
        return stockMapper.selectStockByOwner(owner);
    }

    /**
     * 查询人才股票列表
     *
     * @param stock 人才股票
     * @return 人才股票
     */
    @Override
    public List<Stock> selectStockList(Stock stock) {
        List<Stock> stockList = stockMapper.selectStockList(stock);
        stockList.forEach(i -> {
            Resume resume = iResumeService.getResumeToBelongs(i.getOwner());
            if (null != resume) {
                i.setIssuer(resume.getName());
            }
        });
        return stockList;
    }

    /**
     * 正在发行人才股票
     *
     * @param stock 人才股票
     * @return 人才股票集合
     */
    @Override
    public List<Stock> selectOngoPublishStock(Stock stock) {
        return stockMapper.selectOngoPublishStock(stock);
    }

    /**
     * 完成发行人才股票
     *
     * @param stock 人才股票
     * @return 人才股票集合
     */
    @Override
    public List<Stock> selectFinishPublishStock(Stock stock) {
        return stockMapper.selectFinishPublishStock(stock);
    }

    /**
     * 新增人才股票
     *
     * @param stock 人才股票
     * @return 结果
     */
    @Override
    public int insertStock(Stock stock) {
        stock.setCreateTime(DateUtils.getNowDate());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        stock.setCreateById(user.getUserId());
        stock.setCreateBy(user.getUserName());
        return stockMapper.insertStock(stock);
    }

    /**
     * 修改人才股票
     *
     * @param stock 人才股票
     * @return 结果
     */
    @Override
    public int updateStock(Stock stock) {
        stock.setUpdateTime(DateUtils.getNowDate());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        stock.setUpdateById(user.getUserId());
        stock.setUpdateBy(user.getUserName());
        if (stock.getAmountSum() > 0){
            return stockMapper.updateStock(stock);
        } else {
            stock.setRepurchaseVerify(2);
            return stockMapper.updateStock(stock);
        }

    }

    /**
     * 批量删除人才股票
     *
     * @param ids 需要删除的人才股票主键
     * @return 结果
     */
    @Override
    public int deleteStockByIds(Long[] ids) {
        return stockMapper.deleteStockByIds(ids);
    }

    /**
     * 删除人才股票信息
     *
     * @param id 人才股票主键
     * @return 结果
     */
    @Override
    public int deleteStockById(Long id) {
        return stockMapper.deleteStockById(id);
    }
}
